package com.example.demo03_mvm.view;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.demo03_mvm.R;
import com.example.demo03_mvm.adapter.TypesAdapter;
import com.example.demo03_mvm.databinding.FragmentHomeBinding;
import com.example.demo03_mvm.databinding.FragmentTypeBinding;
import com.example.demo03_mvm.model.GoodsTypeEntity;
import com.example.demo03_mvm.viewmodel.MainViewModel;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TypeFragment#} factory method to
 * create an instance of this fragment.
 */
public class TypeFragment extends BaseFragment<MainViewModel, FragmentTypeBinding> {
    
    
    RecyclerView rv_type;
    TypesAdapter typesAdapter;
    ArrayList<GoodsTypeEntity.DataBean> list = new ArrayList<>();
    @Override
    protected int inflateLayout() {
        return R.layout.fragment_type;
    }

    public static TypeFragment newInstance() {

        Bundle args = new Bundle();

        TypeFragment fragment = new TypeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initData() {
        typesAdapter = new TypesAdapter(R.layout.item_type, list);
        rv_type.setAdapter(typesAdapter);
        rv_type.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));

//        MutableLiveData<GoodsTypeEntity> types = vm.getTypes();
//        types.observe(this, new Observer<GoodsTypeEntity>() {
//            @Override
//            public void onChanged(GoodsTypeEntity goodsTypeEntity) {
//                Log.i("===",goodsTypeEntity.toString());
//                typesAdapter.getData().clear();
//                // list.addAll(goodsTypeEntity.getData());
//                typesAdapter.getData().addAll(goodsTypeEntity.getData());
//                typesAdapter.notifyDataSetChanged();
//                initGoods( typesAdapter.getData().get(0).getId());
//            }
//        });

        typesAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if(typesAdapter.getData().size()!=0){
                    initGoods(typesAdapter.getData().get(position).getId());
                }

            }
        });
    }

    /**
     * 加载商品列表
     * @param tid 类型id
     */
    public void initGoods(int tid){
        //typesAdapter.getData().clear();
        vm.getGoods(tid,this,db);
    }
    public void initView(){
        rv_type=db.rvType;
    }
}